//
//  PopTView.m
//  Project two framework
//
//  Created by 张峥 on 16/7/28.
//  Copyright © 2016年 张峥. All rights reserved.
//

#import "PopTView.h"
#import "ZZConfig.h"
static PopTView *popView = nil;
static UIImageView *bubble;
@interface PopTView ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation PopTView


+ (PopTView *)sharePopView:(CGPoint)centerZ DataSource:(NSMutableArray *)dataSource{
    if (popView == nil) {
        popView = [[PopTView alloc] init];
        popView.center = centerZ;
        popView.bounds = CGRectMake(0, 0, 250, 280);
    }
    popView.center = centerZ;
    bubble.alpha = 1;
    popView.alpha = 1;
    bubble = [[UIImageView alloc] init];
    bubble.bounds = CGRectMake(0, 0, 30, 30);
    bubble.center = CGPointMake(CGRectGetWidth(popView.frame)/2,15);
    bubble.image = [UIImage imageNamed:@"1"];
    [popView addSubview:bubble];
    [popView show:dataSource];
    return popView;
}

- (void)show:(NSMutableArray *)dataSource{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,20, 250, 265) style:UITableViewStylePlain];
    _dataSource = [NSMutableArray arrayWithArray:dataSource];
    tableView.rowHeight = 50;
    tableView.delegate = self;
    tableView.dataSource = self;
    [popView addSubview:tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIndenfier = @"PopTView";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndenfier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndenfier];
    }
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.textLabel.text = _dataSource[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"%@",NSStringFromCGRect(popView.frame));
    NSString    *value1;
    if (popView.frame.origin.x == 340) {
        value1 = @"suppliers";
    }
    if (popView.frame.origin.x == 125) {
        value1 = @"class";
    }
    if (popView.frame.origin.x == 540) {
        value1 = @"income";
    }
    if (popView.frame.origin.x == 755) {
        value1 = @"people";
    }
    NSString *value = _dataSource[indexPath.row];
    NSDictionary *dic;
    if (value1 != nil) {
       dic  = @{@"name":value,@"key":value1};
    }
    NSDictionary *dic1 = @{@"name":value};
    [[NSNotificationCenter defaultCenter] postNotificationName:@"zz" object:nil userInfo:dic1];
    if (value1 != nil) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ghy" object:nil userInfo:dic];
    }

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    bubble.image = [UIImage imageNamed:@"1"];
   [UIView animateWithDuration:0.5 animations:^{
       bubble.image = [UIImage imageNamed:@"1副本"];
       bubble.alpha = 0.2;
       popView.alpha = 0.2;
   } completion:^(BOOL finished) {
           [popView removeFromSuperview];
           [self removeFromSuperview];
   }];
}

@end
